package dimension.starry.gui.screen;

import dimension.starry.StarryDimension;
import dimension.starry.dataManager.PlayerDataManager;
import dimension.starry.gui.Component.AddPointWidget;
import dimension.starry.gui.Component.StatusWidget;
import dimension.starry.player.ComputedPlayerInfo;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.InventoryScreen;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

import static dimension.starry.constants.PlayerDataType.*;

@Environment(EnvType.CLIENT)
public class PlayerInfoScreen extends Screen {

    private static final Identifier PLAYER_INFO_SCREEN_RESOURCE = new Identifier(
        StarryDimension.MOD_ID, "textures/gui/player_info/player_info.png");
    private static final Text TITLE = Text.translatable(StarryDimension.MOD_ID + ".gui.player_info.title");
    private static final int imageWidth = 250;
    private static final int imageHeight = 166;

    private int left;
    private int top;

    public PlayerInfoScreen() {
        super(TITLE);
    }

    @Override
    protected void init() {
        this.left = (this.width - imageWidth) / 2;
        this.top = (this.height - imageHeight) / 2;

        this.addStatusWidget();
        this.addPointWidget();
    }

    private void addStatusWidget() {
        this.addDrawableChild(
            new StatusWidget(
                this.left + 20, this.top + 60, player -> String.valueOf((int) player.getHealth()), "health"));
        this.addDrawableChild(
            new StatusWidget(this.left + 60, this.top + 60, player -> String.valueOf((int) player.getMaxHealth()),
                "max_health"
            ));
        this.addDrawableChild(new StatusWidget(this.left + 100, this.top + 60, player -> String.valueOf(
            PlayerDataManager.getLong(null, MAGIC)), "magic"));
        this.addDrawableChild(new StatusWidget(this.left + 140, this.top + 60,
            player -> String.valueOf(PlayerDataManager.getLong(null, MAX_MAGIC)), "max_magic"
        ));
        this.addDrawableChild(new StatusWidget(this.left + 180, this.top + 60,
            player -> String.valueOf((int) ComputedPlayerInfo.getDefence(player)), "defence"
        ));

        this.addDrawableChild(new StatusWidget(this.left + 20, this.top + 82,
            player -> String.valueOf((int) ComputedPlayerInfo.getDamage(player)), "damage"
        ));
//        this.addDrawable(new StatusWidget(this.left + 60, this.top + 82, player -> String.valueOf(ComputedPlayerInfo.getRemoteDmage(player)), "remote_damage"));
//        this.addDrawable(new StatusWidget(this.left + 100, this.top + 82, player -> String.valueOf(ComputedPlayerInfo.getMagicDamage(player)), "magic_damage"));
        this.addDrawableChild(new StatusWidget(this.left + 140, this.top + 82,
            player -> String.valueOf(ComputedPlayerInfo.getMagicRegen(player)), "magic_regen"
        ));
//        this.addDrawable(new StatusWidget(this.left + 180, this.top + 82, player -> String.valueOf(ComputedPlayerInfo.getExpBonus(player)), "exp_bonus"));

        this.addDrawableChild(new StatusWidget(this.left + 20, this.top + 104,
            player -> (int) (ComputedPlayerInfo.getCriticalChance(player) * 100) + "%", "crit_chance"
        ));
        this.addDrawableChild(new StatusWidget(this.left + 60, this.top + 104,
            player -> (int) (ComputedPlayerInfo.getCriticalDamage(player) * 100) + "%", "crit_damage"
        ));
//        this.addDrawable(new StatusWidget(this.left + 100, this.top + 104, player -> String.valueOf(ComputedPlayerInfo.getSearchAbility(player)), "search_ability"));
        this.addDrawableChild(new StatusWidget(this.left + 140, this.top + 104,
            player -> String.valueOf(PlayerDataManager.getLong(null, STAMNA)), "stamina"
        ));
        this.addDrawableChild(new StatusWidget(this.left + 180, this.top + 104,
            player -> String.valueOf(ComputedPlayerInfo.getMaxStamina(player)), "max_stamina"
        ));
    }

    private void addPointWidget() {
        this.addDrawableChild(new AddPointWidget(this.left + 70, this.top + 5, STRENGTH));
        this.addDrawableChild(new AddPointWidget(this.left + 90, this.top + 5, STRONG));
        this.addDrawableChild(new AddPointWidget(this.left + 110, this.top + 5, AGILE));
        this.addDrawableChild(new AddPointWidget(this.left + 130, this.top + 5, WISDOM));
        this.addDrawableChild(new AddPointWidget(this.left + 150, this.top + 5, ANALYSIS));
        this.addDrawableChild(new AddPointWidget(this.left + 170, this.top + 5, FLEXIBLE));
    }

    @Override
    public void render(DrawContext drawContext, int mouseX, int mouseY, float particleTick) {
        drawContext.fillGradient(0, 0, this.width, this.height, -1072689136, -804253680);
        drawContext.drawTexture(
            PLAYER_INFO_SCREEN_RESOURCE, this.width / 2 - imageWidth / 2, 20, 0, 0, imageWidth, imageHeight, imageWidth,
            imageHeight
        );

        ClientPlayerEntity player = MinecraftClient.getInstance().player;
        drawContext.drawTextWithShadow(
            MinecraftClient.getInstance().textRenderer,
            Text.translatable(StarryDimension.MOD_ID + ".gui.player_info.label.attribute_points"), left + 70, top - 6,
            16777215
        );
        drawContext.drawTextWithShadow(
            MinecraftClient.getInstance().textRenderer,
            String.valueOf(PlayerDataManager.getInt(player, ATTRBUTE_POINTS)), left + 135, top - 6, 16777215
        );

        InventoryScreen.drawEntity(
            drawContext, (this.width - imageWidth) / 2 + 40, (this.height - imageHeight) / 2 + 45, 25,
            (this.width - imageWidth) / 2 - mouseX, (this.height - imageHeight) / 2 - mouseY,
            MinecraftClient.getInstance().player
        );
        super.render(drawContext, mouseX, mouseY, particleTick);
    }

}
